package controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.record.formula.functions.Rows;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.*;
/*
import org.zkoss.zul.Div;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;
*/

public class DirectoryController extends SelectorComposer<Window> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7749686032257976761L;

	
	
	@Wire
	private Treechildren treeRoot;

	@Wire
	private org.zkoss.zul.Rows rows;
	
	@Wire
	private Grid grid;
	
	@Override
	public void doAfterCompose(Window comp) throws Exception {
		super.doAfterCompose(comp);
		
		java.io.File root = new File("C://");
		getChild(root, treeRoot);
		
		getFile(root);
		
	}
	
	private void getChild(File parent,Treechildren root){
		Treeitem item;
		Treerow row;
		Treecell cell;
		File[] arrFile = parent.listFiles();
		for(int i = 0;i<arrFile.length;i++){
			File f = arrFile[i];
			if(f.isDirectory() && f.isHidden() == false){
				
				item = new Treeitem();
				//item.setOpen(false);
				row = new Treerow();
				cell = new Treecell();
				
				cell.setLabel(f.getName());
				cell.setTooltip(f.getPath());
				row.appendChild(cell);
				item.appendChild(row);
				
				if(haveSubDirectory(f)){
					Treechildren child = new Treechildren();
					item.appendChild(child);
					
					getChild(f, child);
				}
				
				root.appendChild(item);
			}
			
		}
	}
	
	private boolean haveSubDirectory(File file){
		File[] arrFile = file.listFiles();
		for(int i = 0;i< arrFile.length;i++){
			if(arrFile[i].isDirectory() && arrFile[i].isHidden() == false){
				return true;
			}
		}
		return false;
	}
	
	private void getFile(File root){
		File[] arrFile = root.listFiles();
		Row row;
		Checkbox checkbox;
		Label name;
		Label size;
		Label updateDate;
		Label author;
		Label version;
		for(int i = 0;i<arrFile.length;i++){
			File f = arrFile[i];
			if(f.isFile()){
				row = new Row();
				checkbox = new Checkbox();
				name = new Label(f.getName());
				size = new Label(f.getTotalSpace() + "");
				
				Date dNow = new Date( );
				SimpleDateFormat ft = new SimpleDateFormat ("dd-MM-yyyy");
				
				updateDate = new Label(ft.format(dNow));
				author = new Label("ANGUYEN");
				version = new Label("1");
				
				row.appendChild(checkbox);
				row.appendChild(name);
				row.appendChild(size);
				row.appendChild(updateDate);
				row.appendChild(author);
				row.appendChild(version);
				
				rows.appendChild(row);
				
			}
			
		}
	}
	
	@Listen("onClick = treecell")
	public void click(Event event){
		
		while(rows.getChildren().size() > 0){
			rows.removeChild(rows.getChildren().get(0));
		}
		
		
		getFile(new File(((Treecell)event.getTarget()).getTooltip()));
	}
	
	
}
